package ellements;

import java.util.Random;

import ellements.drawable.Desk;

import sounds.MyMedia;
import matrix.world.volleyball.MySurfaceView;
import matrix.world.volleyball.R;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;

public class CrabEnemy extends Crab {

	private Random rd = new Random();
	private int t = -1;
	private int tjump = -1;
	
	public CrabEnemy(Resources res) {
		super(res);
		mBitmap = BitmapFactory.decodeResource(res, R.drawable.crabb);
		
		Matrix matrix = new Matrix();
		matrix.postScale(MySurfaceView.rate, MySurfaceView.rate);
		mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), matrix, true);

		x = MySurfaceView.Width/4*3;
		tox=x;
	}
	
	public void animation(Ball ball){
		vy+=Gravity;
		y+=vy;
		if (y>Floor){
			if (isJump) nextframe();
			isJump = false;
			y=Floor;
			vy*=DUMCEG;
			
		}
		
		x=x+(tox-x)/squence;
		if (squence>1) squence--;
		if (x<Desk.NetRight+radius*0.6f) 
			x=Desk.NetRight+radius*0.6f; 
		else if (x>MySurfaceView.Width-radius) 
			x=MySurfaceView.Width-radius;
		
		if (t<0){
			t=rd.nextInt(5);
			if (ball.x>MySurfaceView.Width/2){
				tox = ball.x+rd.nextInt((int)(2*radius))-radius;
			}
			squence = 10;
		}else{
			t--;
		}
		
		if (tjump<0){
			if (ball.y>200*MySurfaceView.rate && ball.x>x-radius && ball.x<x){
				tjump=rd.nextInt(1000);
				jump();
			}
		}else{
			tjump--;
		}

		if (xold-x>2 || xold-x<-2){
			xold = x;
	        iframe++;
	        if (iframe>2) {
	        	iframe=0;
	        	MyMedia.playMove();
	        }
		}
		
		
			
	}
	
	

	
	
}
